#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    :   start.py    
@Contact :   raogx.vip@hotmail.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA

@Modify Time      @Author          @Version    @Desciption
------------      -------------    --------    -----------
2020/3/12 11:22 PM   Weiping Huang      1.0         None
"""

from flask import Flask  #导入flask
import json
from flask import request
import requests
from personcheck import people_check


app = Flask(__name__)

path_fanart = '/image_flask' + '/source/'

def download_img(img_url,media_name):
    print (img_url)
    header = {"Authorization": "Bearer "} # 设置http header，视情况加需要的条目，这里的token是用来鉴权的一种方式
    response = requests.get(img_url, headers=header, stream=True,verify = False)
    if response.status_code == 200:
        with open(path_fanart + media_name + ".jpg", 'wb') as f:
            f.write(response.content)
            return path_fanart + media_name + ".jpg"



@app.route('/',methods=['GET', 'POST'])  #装饰器，用作url与视图函数的映射，一般写在函数上方
def index():
    if request.method == 'POST':
        data = request.get_data()
        dict1 = json.loads(data)
        url = dict1["url"]
        media_name = dict1["media_name"]
        print(path_fanart)
        source_path = download_img(url,media_name)
        people_check(source_path,media_name)
        return json.dumps("http://127.0.0.1:5000/static/" + media_name + ".jpg")
    else:
        return 'ss'


if __name__ == '__main__':  #主函数，运行后在浏览器输入127.0.0.1:5000(或者进入控制台显示的地址即可)，app.run内可以指定参数，比如端口号和debug等，不指定则使用默认。
    app.run(host='0.0.0.0',ssl_context='adhoc')